package com.live.shop.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ProductGroupModel  implements Serializable {
    int productGroupId;//产品组ID
    String productGroupName;//产品组名称
    int productGroupStatus;//启用状态 0未启用 1启用
    String createTime;//创建时间
    List<ProductMaterialRecordModel> listRecord;

    public static JSONObject model2Json(ProductGroupModel model){
        JSONObject json = new JSONObject();
        try {
            json.put("productGroupId",model.getProductGroupId());
            json.put("productGroupName",model.getProductGroupName());
            json.put("productGroupStatus",model.getProductGroupStatus());
            json.put("createTime",model.getCreateTime());
            JSONArray recordArray = new JSONArray();
            for (ProductMaterialRecordModel m:model.getListRecord()) {
                recordArray.put(m.model2Json(m));
            }
            json.put("listRecord",recordArray);

        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    public static ProductGroupModel Json2model(JSONObject json){
        ProductGroupModel model = new ProductGroupModel();
        try {
            model.setProductGroupId(json.getInt("productGroupId"));
            model.setProductGroupName(json.getString("productGroupName"));
            model.setProductGroupStatus(json.getInt("productGroupStatus"));
            model.setCreateTime(json.getString("createTime"));

            List<ProductMaterialRecordModel> list = new ArrayList<>();

            JSONArray recordArray = json.getJSONArray("listRecord");
            for (int i =0 ; i < recordArray.length();i++) {
                ProductMaterialRecordModel reModel = new ProductMaterialRecordModel();
                JSONObject recordJson = recordArray.getJSONObject(i);

                reModel.setId(recordJson.getInt("id"));
                reModel.setProductGroupId(recordJson.getInt("productGroupId"));
                reModel.setMaterialName(recordJson.getString("materialName"));
                reModel.setMaterialType(recordJson.getInt("materialType"));
                reModel.setMaterialFilepath(recordJson.getString("materialFilepath"));
                reModel.setMaterialFileuri(recordJson.getString("materialFileuri"));
                reModel.setLocationX(recordJson.getInt("locationX"));
                reModel.setLocationY(recordJson.getInt("locationY"));
                reModel.setScaleRate(recordJson.getInt("scaleRate"));
                reModel.setHideState(recordJson.getInt("hideState"));
                reModel.setMaterialShowState(recordJson.getInt("materialShowState"));
                reModel.setMaterialKeyShowtime(recordJson.getInt("materialKeyShowtime"));
                reModel.setKeyword(recordJson.getString("keyword"));
                reModel.setAnswerTxt(recordJson.getString("answerTxt"));
                reModel.setAnswerType(recordJson.getInt("answerType"));
                reModel.setAnswerState(recordJson.getInt("answerState"));
                reModel.setAnswerAudio(recordJson.getString("answerAudio"));
                reModel.setCreateTime(recordJson.getString("createTime"));
                list.add(reModel);
            }
            model.setListRecord(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return model;
    }

    public int getProductGroupId() {
        return productGroupId;
    }

    public void setProductGroupId(int productGroupId) {
        this.productGroupId = productGroupId;
    }

    public String getProductGroupName() {
        return productGroupName;
    }

    public void setProductGroupName(String productGroupName) {
        this.productGroupName = productGroupName;
    }

    public int getProductGroupStatus() {
        return productGroupStatus;
    }

    public void setProductGroupStatus(int productGroupStatus) {
        this.productGroupStatus = productGroupStatus;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public List<ProductMaterialRecordModel> getListRecord() {
        return listRecord;
    }

    public void setListRecord(List<ProductMaterialRecordModel> listRecord) {
        this.listRecord = listRecord;
    }
}
